package Interfaces;

import java.awt.BorderLayout;
import java.awt.Font;
import java.awt.GridLayout;
import java.awt.Image;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.File;

import javax.swing.AbstractButton;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JFileChooser;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JMenu;
import javax.swing.JMenuBar;
import javax.swing.JMenuItem;
import javax.swing.JOptionPane;
import javax.swing.JPanel;

import Boundary.EasySM;
import DataView.Message;

public class ShowWelcome {
	JFrame window;
	public JPanel container;
	ShowWelcome self=this;
	EasySM easySM;
	
	ActionListener ListenerNuovoProgetto = new ActionListener()
	{
		@Override public void actionPerformed(ActionEvent arg0) 
		{
			String str = JOptionPane.showInputDialog(container, "Insert the Name of your project: ", "Insert Name");
	        if(str!=null)
	        {
	        	if(str.isEmpty())
	        		JOptionPane.showMessageDialog(container, "Error: no name inserted!!");
	        	else
	        	{
	        		container.removeAll();
		        	easySM.newProject(str);
	        	}
	        }	
		}
	};
	ActionListener ListenerApriProgetto = new ActionListener()
	{
		@Override public void actionPerformed(ActionEvent arg0) 
		{
			JFileChooser fc = new JFileChooser();
			fc.setCurrentDirectory(new File("."));
			int returnVal = fc.showOpenDialog(window);

            if (returnVal == JFileChooser.APPROVE_OPTION) 
            {
                File file = fc.getSelectedFile();
                
                container.removeAll();
                easySM.openProject(file.getName());
            }
		}
	};
	
	public void showWelcome(EasySM program)
	{
		this.easySM = program;
		window = new JFrame();
		JPanel superContainer = new JPanel();
		window.setContentPane(superContainer);
		superContainer.setLayout(new BorderLayout());
		superContainer.add(getMenuPrincipale(),BorderLayout.NORTH);
		superContainer.add(getCentralArea(), BorderLayout.CENTER);
		window.setSize(800, 600);
		window.setTitle("the I-Team - EasySM");
		Image i=java.awt.Toolkit.getDefaultToolkit().getImage("src/Images/logo.png");
		window.setIconImage(i);
		window.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		window.setVisible(true);
		
	}
	
	public JMenuBar getMenuPrincipale()
	{
		JMenuBar barraMenu = new JMenuBar();
		
		JMenu File = new JMenu("File");
		JMenuItem save = new JMenuItem("Save");
		JMenuItem chiudi = new JMenuItem("Close");
		chiudi.addActionListener(new ActionListener() 
			{
			@Override public void actionPerformed(ActionEvent e) 
				{
					easySM.main.doClose();
				}
			});
		save.addActionListener(new ActionListener() 
			{
			@Override public void actionPerformed(ActionEvent e) 
				{
					easySM.main.doSave();
				}
			});
		File.add(save);
		File.addSeparator();
		File.add(chiudi);		
		
		JMenu Help = new JMenu("Help");
		JMenuItem credits = new JMenuItem("Credits...");
		credits.addActionListener(new ActionListener()
		{
			@Override public void actionPerformed(ActionEvent arg0) 
			{
        		JOptionPane.showMessageDialog(container, "An I-TEAM Project, developed in University of Computer Science Genova (DISI)\nMembers :\n" +
        				"Salvini Melchise 'Faceman'\nMendoza Horacio 'B.A.'\nNaso Rappis 'Howling Mad'", "Credits", JOptionPane.PLAIN_MESSAGE);
			}
		});
		
		JMenuItem info = new JMenuItem("Project Text...");
		info.addActionListener(new ActionListener()
		{
			@Override public void actionPerformed(ActionEvent arg0) 
			{
        		JOptionPane.showMessageDialog(container, "The program implements the following functionalities : \n" +
        				"\tS) The program generates a valid State Machine\n\tM) The program implements error check on Class Diagram, State Observers and Events\n\tL) The program implements the validation/unvalidation (missing)",
        				 "Project Text", JOptionPane.PLAIN_MESSAGE);
			}
		});
		Help.add(credits);
		Help.add(info);
		
		barraMenu.add(File);
		barraMenu.add(Help);		
		
		return barraMenu;
	}
	
	public JPanel getCentralArea()
	{
		container = new JPanel();
			JPanel southArea= new JPanel();
			JButton crea =new JButton("New...", new ImageIcon("src/Images/new_small.png"));
			JButton apri =new JButton("Open...", new ImageIcon("src/Images/open_small.png"));
			crea.setVerticalTextPosition(AbstractButton.BOTTOM);
			crea.setHorizontalTextPosition(AbstractButton.CENTER);
			apri.setVerticalTextPosition(AbstractButton.BOTTOM);
			apri.setHorizontalTextPosition(AbstractButton.CENTER);
			crea.addActionListener(ListenerNuovoProgetto);
			apri.addActionListener(ListenerApriProgetto);
		
		container.setLayout(new BorderLayout());
			
		southArea.setLayout(new GridLayout(3,6));
		southArea.add(new JLabel(""));
		southArea.add(new JLabel(""));
		southArea.add(new JLabel(""));
		southArea.add(new JLabel(""));
		southArea.add(new JLabel(""));
		southArea.add(new JLabel(""));
		southArea.add(new JLabel(""));
		southArea.add(crea);
		southArea.add(new JLabel(""));
		southArea.add(new JLabel(""));
		southArea.add(apri);
		southArea.add(new JLabel(""));
		southArea.add(new JLabel(""));
		southArea.add(new JLabel(""));
		southArea.add(new JLabel(""));
		southArea.add(new JLabel(""));
		southArea.add(new JLabel(""));
		southArea.add(new JLabel(""));
		
		container.add(getCenterArea(), BorderLayout.NORTH);
		container.add(southArea, BorderLayout.CENTER);
		
		return container;
	}
	
	public JPanel getCenterArea()
	{
		JPanel pannello = new JPanel();
		pannello.setLayout(new GridLayout(6,1));
		JLabel l1 = new JLabel("Welcome", JLabel.CENTER);
		l1.setFont(new Font("Tahoma", Font.BOLD, 32));
		JLabel l2 = new JLabel("in EasyStateMachine", JLabel.CENTER);
		l2.setFont(new Font("Tahoma", Font.BOLD, 24));
		pannello.add(new JLabel(""));
		pannello.add(new JLabel(""));
		pannello.add(l1);
		pannello.add(l2);
		pannello.add(new JLabel(""));
		pannello.add(new JLabel(""));
		return pannello;
	}

	public void showMessage(Message message) 
	{
		String mex;
		switch (message)
		{
		case fileNotFound : mex="Error: File not Found!"; break;
		case noContextclass : mex="Error: Context Class is missing"; break;
		default : mex="Error: Generic Error";
		}
		
		JOptionPane.showConfirmDialog(window, mex);
	}
}
